package com.dyit.controllor;


import com.dyit.dto.HttpResp;
import com.dyit.entity.Book;
import com.dyit.entity.Category;
import com.dyit.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;

@RestController
@RequestMapping("/api/book")
public class BookController {

    @Autowired
    private IBookService ibs;

    @GetMapping("/findAll")
    public HttpResp findAll(){
        return new HttpResp(2001,"查询成功",ibs.findAll(), LocalDateTime.now());
    }

    @PostMapping("/insert")
    public HttpResp insert(@RequestBody Book book){
        if (ibs.add(book)){
            return new HttpResp(2001,"添加成功",null, LocalDateTime.now());
        }else {
            return new HttpResp(2002,"添加失败",null, LocalDateTime.now());
        }
    }

    @DeleteMapping("/delete")
    public HttpResp delete(@RequestParam("id") int id){
        ibs.deleteById(id);
        return new HttpResp(
                2001,
                "删除成功",
                null,
                LocalDateTime.now()
        );
    }
}
